package Stack;

import org.junit.Test;

import java.util.*;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Stack
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-21  11:16
 * @Description: TODO
 * @Version: 1.0
 */
public class 下一个更大元素II503 {

    public int[] nextGreaterElements(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        Arrays.fill(result, -1);
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < n * 2 - 1; i++) {
            // 利用单调栈 一般单调栈和数组下标有关系 然后在通过数据下标来实现对的元素的获取。
            while (!stack.isEmpty() && nums[i % n] > nums[stack.peek()]) {
                int index = stack.pop();
                result[index] = nums[i % n];
            }
            stack.push(i % n);
        }
        return result;
    }

    @Test
    public void test() {
        int[] ints = nextGreaterElements(new int[]{1, 2, 3, 4, 3});
        System.out.println(Arrays.toString(ints));
    }

}
